package info.motteke.qjdbc.column;

import info.motteke.qjdbc.column.filter.BetweenFilter;
import info.motteke.qjdbc.column.filter.EqualFilter;
import info.motteke.qjdbc.column.filter.GreaterFilter;
import info.motteke.qjdbc.column.filter.GreaterOrEqualFilter;
import info.motteke.qjdbc.column.filter.InFilter;
import info.motteke.qjdbc.column.filter.LessFilter;
import info.motteke.qjdbc.column.filter.LessOrEqualFilter;
import info.motteke.qjdbc.column.variable.IntVariable;
import info.motteke.qjdbc.exception.SqlException;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 整数値を表すメタデータです。
 *
 * @author YWak
 */
public final class IntMeta extends Meta {

    public IntMeta(String name) {
        super(name);
    }

    public EqualFilter equal(int value) {
        return equal(new IntVariable(value));
    }

    public EqualFilter equal(IntVariable var) {
        return new EqualFilter(var);
    }

    public GreaterFilter greaterThan(int value) {
        return greaterThan(new IntVariable(value));
    }

    public GreaterFilter greaterThan(IntVariable var) {
        return new GreaterFilter(var);
    }

    public GreaterOrEqualFilter greaterThanOrEqual(int value) {
        return greaterThanOrEqual(new IntVariable(value));
    }

    public GreaterOrEqualFilter greaterThanOrEqual(IntVariable var) {
        return new GreaterOrEqualFilter(var);
    }

    public LessFilter lessThan(int value) {
        return lessThan(new IntVariable(value));
    }

    public LessFilter lessThan(IntVariable var){
        return new LessFilter(var);
    }

    public LessOrEqualFilter lessThanOrEqual(int value) {
        return lessThanOrEqual(new IntVariable(value));
    }

    public LessOrEqualFilter lessThanOrEqual(IntVariable var){
        return new LessOrEqualFilter(var);
    }

    public BetweenFilter between(int start, int end) {
        return between(new IntVariable(start), new IntVariable(end));
    }

    public BetweenFilter between(IntVariable start, IntVariable end){
        return new BetweenFilter(start, end);
    }

    public InFilter in(int... values) {
        IntVariable[] vars = new IntVariable[values.length];

        for (int i = 0; i < values.length; i++) {
            vars[i] = new IntVariable(values[i]);
        }

        return in(vars);
    }

    public InFilter in(IntVariable... vars){
        return new InFilter(vars);
    }

    public IntVariable variable() {
        return new IntVariable();
    }

    int getValue(ResultSet rs){
        try {
            return rs.getInt(getName());
        } catch (SQLException e) {
            throw new SqlException(e);
        }
    }
}
